﻿using Core.Framework;
using DotNetty.Framework;
using DotNetty.Framework.BaseTriggers;
using iTool.Connector.CommandOfParameter;
using Grain.Interfaces;
using iTool.ClusterComponent;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace iTool.Connector.CommandWorkers
{
    [Worker("iTool Reminder")]
    public class RemindersWorker : WorkerController
    {

        private iToolClusterHostClient iCluster;
        public RemindersWorker(iToolClusterHostClient cluster)
        {
            this.iCluster = cluster;
        }

        [MethodRoute("Get")]
        public async Task<DoWorkResponse> Get(HandleDoWorkingContext context)
        {
            var iReminders = this.iCluster.GetService<IReminderService>();
            var list = await iReminders.GetReminderList();

            List<object> reminderResult = new List<object>();

            foreach (var item in list)
            {
                string lastRunTimeStr = string.Empty;

                if (item.Value.LastRunTime != null)
                {
                    var timeSpan = DateTime.Now - item.Value.LastRunTime;

                    lastRunTimeStr = timeSpan?.Minutes > 0 ?
                        timeSpan?.Minutes + " 分钟前"
                        :
                        timeSpan?.Seconds + " 秒钟前";
                }

                reminderResult.Add(new
                {
                    ReminderName = item.Key,
                    StartTime = item.Value.RegTime,
                    PeriodOfMinutes = $"{item.Value.PeriodOfMinutes} 分钟",
                    LastRunTime = lastRunTimeStr,
                    item.Value.IsRunSuccess,
                    item.Value.ErrorInfo,
                    FunctionModuleName = item.Value.FunctionModuleName.Replace("./CloudFunctions/", "")
                });
            }
            return this.GetWorkResponse(context, reminderResult);
        }


        [MethodRoute("Register")]
        public async Task<DoWorkResponse> Register(HandleDoWorkingContext context)
        {
            string result = string.Empty;
            var parameter = context.Message.GetParameterModel<RemindersParameter>();
            var iReminders = this.iCluster.GetService<IReminderService>();

            parameter.ModuleName = "./CloudFunctions/" + parameter.ModuleName;

            if (await iReminders.IsExistByModulePath(parameter.ModuleName))
            {
                result = "0";
            }
            else if (await iReminders.IsExistByReminderName(parameter.ReminderName))
            {
                result = "2";
            }
            else
            {
                await iReminders.DoSomethingThatTriggersReminder(
                   parameter.ReminderName,
                   parameter.ModuleName,
                   parameter.PeriodOfMinutes);
                result = "1";
            }
            return this.GetWorkResponse(context, result);
        }


        [MethodRoute("Un")]
        public async Task<DoWorkResponse> Un(HandleDoWorkingContext context)
        {
            var iReminders = this.iCluster.GetService<IReminderService>();
            await iReminders.UnregisterReminder(context.Message.Parameter);
            return this.GetWorkResponse(context, "1");
        }
    }
}
